class GoodsList{
	constructor(){
		this.url = "http://localhost/dist/static/alldata.json";
		this.leftBox = document.querySelector(".leftBox")
		this.btn = document.querySelector(".details .buy .addCar");
		this.oImg = document.querySelector(".leftBox img");
		this.load();
		
	}
	load(){
		ajax({
			url:this.url
		}).then((res)=>{
			this.res = JSON.parse(res);
			this.addEvent();
		});
	}
	addEvent(){
		var that = this;
		this.btn.onclick = function(){
			that.goodsId = that.oImg.getAttribute("goodsId");
			that.setData();
		}
	}
	setData(){
		var gm = localStorage.getItem("goodsMsg") ? JSON.parse(localStorage.getItem("goodsMsg")) : [];
		if(gm.length < 1){
			gm.push({
				goodsId:this.goodsId,
				num:1,
				msg:this.getDate(this.goodsId)
			});
		}else{
			var k;
			var flag = gm.some((val,idx)=>{
				k = idx;
				return val.goodsId == this.goodsId;
			});
			if(flag){
				gm[k].num++;
			}else{
				gm.push({
					goodsId:this.goodsId,
					num:1,
					msg:this.getDate(this.goodsId)
				});
			}
		}
		localStorage.setItem("goodsMsg",JSON.stringify(gm));
	}
	getDate(id){
		for(var x=0;x<this.res.length;x++){
			if(this.res[x].goodsId == id){
				return this.res[x];
			}
		}
		return {};
	}
}
